<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use think\facade\Db;
class Goods
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
		$res = Db::name("goods")->alias("g")->join("goods_category gc","g.id=gc.goodsId")->field("name,img,sale_cnt,price,g.createTime,g.updateTime,g.id,g.info,gc.categoryId")->select();
		return json(["code"=>200,"data"=>$res]);
    }
	public function queryCategory(){
		$res = Db::name("category")->where("pid",0)->select();
		foreach($res as $key=>$value){
			$arr["children"] = Db::name("category")->where("pid",$value["_id"])->select();
			$res[$key] = array_merge($arr,$res[$key]);
		}
		return json(["code"=>200,"data"=>$res]);
	}
	public function addCategory(){
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		$arr = ["createTime"=>$date,"updateTime"=>$date];
		Db::name("category")->save($p+$arr);
		return json(["code"=>200,"msg"=>"操作成功"]);
	}
	public function delCategory(){
		$p = input("get.");
		Db::name("category")->where("_id",$p["_id"])->delete();
		Db::name("category")->where("pid",$p["_id"])->delete();
		return json(["code"=>200,"msg"=>"操作成功"]);
	}
	public function updateCategory(){
		$p = input("post.");
		Db::name("category")->where("_id",$p["_id"])->update(["name"=>$p["name"],"img"=>$p["img"]]);
		return json(["code"=>200,"msg"=>"操作成功"]);
	}
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		$id = Db::name("goods")->insertGetId(["name"=>$p["name"],"img"=>$p["url"],"price"=>$p["price"],"sale_cnt"=>$p["num"],"createTime"=>$date,"updateTime"=>$date,"info"=>$p["info"]]);
		Db::name("goods_category")->insert(["goodsId"=>$id,"categoryId"=>$p["categoryvalue"],"createTime"=>$date,"updateTime"=>$date]);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        //
		$res = Db::table("goods")->select();
		return json(["code"=>200,"data"=>$res]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        //
		
		$p = input("post.");
		// print_r($p);
		$date = date('Y-m-d H:i:s',time());
		$sql="update goods set info='".$p["info"]."',name='".$p["name"]."',img='".$p["url"]."',sale_cnt=".(int)$p["num"].",price=".$p["price"].",updateTime='".$date."'  where id=".$p["goodsId"];
		print_r($sql);
		Db::execute($sql);
		// Db::name("goods")->update(["info"=>$p["info"],"id"=>$p["goodsId"],"name"=>$p["name"],"img"=>$p["url"],"sale_cnt"=>(int)$p["num"],"price"=>$p["price"],"updateTime"=>$date]);
		Db::name("goods_category")->where("goodsId",$p["goodsId"])->update(["categoryId",$p["categoryvalue"]]);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //
		$p = input("get.");
		Db::name("goods")->where("id",$p["id"])->delete();
		Db::name("goods_category")->where("goodsId",$p["id"])->delete();
		return json(["code"=>200,"msg"=>"操作成功"]);
    }
}
